<?php

class Nuo_View_Parser
{
    /**
     * get the block engine to parse content to xhtml
     * @param array $region
     * @param array $areas
     * @return string
     */
    public function renderRegion($areas, $layout = array())
    {
        $xhtml = array();

        foreach ($areas as $name => $blocks)
        {
            $xhtml["{$name}"] = $this -> renderArea($blocks);
        }

        // column
        $method = 'layoutGrid' . nuo_inflect_string($layout['layout_grid']);

        if (!method_exists($this, $method))
        {
            $method = 'layoutGridSimple';
        }

        return $this -> {$method}($xhtml, $layout);
    }

    /**
     * render a regions
     * @param array $blocks  list of block elements
     * @return string xhtml string
     */
    public function renderArea($blocks)
    {
        $xhtml = '';
        foreach ($blocks as $block)
        {
            $xhtml .= '<div class="' . $block['css_class'] . '" id="' . $block['css_id'] . '">' . $this -> renderBlock($block) . '</div>';
        }
        return $xhtml;
    }

    /**
     * render a block
     * @param array $block  block element
     * @return string xhtml string
     */
    public function renderBlock($block)
    {
        list($controllerName, $actionName) = explode(':', $block['block_class']);
        return Request::execute(array(), $controllerName, $actionName);
    }

    /**
     * parse region none
     * @param array $areas
     * @param array $region
     * @return string xhtml string
     */
    public function layoutGridSimple($areas, $region)
    {
        $xhtml = '<div class="uiRegion uiRegionNone">';

        $xhtml .= '<div class="uiArea uiAreaMain">' . @$areas['main'] . '</div>';

        $xhtml .= '</div>';

        return $xhtml;
    }

    /**
     * parse region one left
     * @param array $areas
     * @param array $region
     * @return string xhtml string
     */
    public function layoutGridOneLeft($areas, $region)
    {
        $hasLeft = isset($areas['column']) && !empty($areas['column']);

        $regionClass = $hasLeft ? 'uiRegionOneLeft' : 'uiRegionNone';

        /**
         * xhtml result
         * @var string
         */
        $xhtml = '<div class="uiRegion uiRegionOneLeft">';

        if ($hasLeft)
        {
            $xhtml .= '<div class="uiArea uiAreaLeft">' . $areas['column'] . '</div>';
        }

        $xhtml .= '<div class="uiArea uiAreaMain">' . @$areas['main'] . '</div>';

        $xhtml .= '</div>';

        return $xhtml;

    }

    /**
     * parse region one left
     * @param array $areas
     * @param array $region
     * @return string xhtml string
     */
    function layoutGridOneRight($areas, $region)
    {
        $hasRight = isset($areas['column']) && !empty($areas['column']);

        $regionClass = $hasRight ? 'uiRegionOneLeft' : 'uiRegionNone';

        /**
         * xhtml result
         * @var string
         */
        $xhtml = '<div class="uiRegion uiRegionOneLeft">';

        if ($hasRight)
        {
            $xhtml .= '<div class="uiArea uiAreaRight">' . $areas['column'] . '</div>';
        }

        $xhtml .= '<div class="uiArea uiAreaMain">' . $areas['main'] . ' </div>';

        $xhtml .= '<div style="clear:both"><div></div>';

        return $xhtml;

    }

}
